package com.foreveross.action;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foreveross.common.CommonUtil;
import com.foreveross.common.Constants;
import com.foreveross.service.LogService;
import com.foreveross.service.VisitorCheckInService;
import com.foreveross.util.AuthUtil;
import com.foreveross.vo.HouseRepair;
import com.foreveross.vo.HouseUser;
import com.foreveross.vo.HrHouses;
import com.foreveross.vo.TenementCost;
import com.foreveross.vo.VisitorCheckIn;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;
import com.foreveross.weixin.service.QrcodeTicketService;
import com.foreveross.weixin.vo.Qrcode.ReturnQrcodeVo;

@Controller
@RequestMapping(value = "/sysVisitorCheckInController")
public class SysVisitorCheckInController extends BaseCostroller {

	private Logger log = Logger.getLogger(SysVisitorCheckInController.class);
	@Resource
	private VisitorCheckInService visitorCheckInService;
	@Resource
	private LogService logService;
 

	/**
	 * 访客登记查看
	 * 
	 * @param request
	 * @param response
	 * @param name
	 * @param phone
	 * @param user_id
	 * @param OnePageCount
	 * @param currentPage 
	 */

	@RequestMapping(method = RequestMethod.POST, value = "/getVisitorCheckIn")
	public void getVisitorCheckIn(HttpServletRequest request,
			HttpServletResponse response, String name, String phone,
			String user_id,String rows, String page, String houseId,String code,String userId) {
		ResultVo resultVo = new ResultVo();
		try {
			int countRecord = (int) visitorCheckInService
					.getVisitorCheckInListCount(name, phone, user_id,houseId, code,null,userId);
			PageVo<VisitorCheckIn> pageObj = new PageVo<VisitorCheckIn>(
					page, countRecord, rows);
			pageObj.setRows(visitorCheckInService.findVisitorCheckInList(name,
					phone, user_id, houseId, code,null, pageObj.getpageIndex(),
					pageObj.getpageSize(),userId));
			resultVo.setData(pageObj);
			resultVo.setSuccess(true);
			log.info("访客登记 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("访客登记  查看失败");
			log.error("访客登记 查看失败!", e);
		}
	    //List list = Arrays.asList("houseUser");
		List listz = Arrays.asList("messageHouses","newsHouses","optimizedHouses");
		CommonUtil.PrintWriterResultWithJSON(response, resultVo,listz);
	}
	

	/**
	 * 访客登记添加修改
	 * 
	 * @param request
	 * @param response
	 * @param express
	 * @param updateType
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/addOrUpdateVisitorCheckIn")
	public void addOrUpdateVisitorCheckIn(HttpServletRequest request,
			HttpServletResponse response, VisitorCheckIn visitorCheckIn,
			String updateType,String user_id) {
		ResultVo resultVo = new ResultVo();
		try {
			//visitorCheckIn.setHouseUser(new HouseUser(user_id));
			visitorCheckInService.saveOrUpdate(visitorCheckIn, updateType);
			
			ReturnQrcodeVo returnQrcodeVo = QrcodeTicketService.getQrcodeTicket(visitorCheckIn);
			visitorCheckIn.setTicket(returnQrcodeVo.getTicket());
			resultVo.setSuccess(true);
			resultVo.setData(visitorCheckIn);
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,
					updateType.equals(Constants.UPDATE_ADD) ? Constants.LOG_ADD
							: Constants.LOG_UPDATE,
					"访客登记" + Constants.getUpdateType(updateType), AuthUtil
							.getUserid(request),
					Constants.getUpdateType(updateType) + "访客登记["
							+ visitorCheckIn.toString() + "]");
			log.info("访客登记 " + updateType + "成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("访客登记 " + updateType + "失败");
			log.error("访客登记" + updateType + "失败!", e);
		}

		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	/**
	 * 
	 * 查看访客登录详情
	 * 
	 * @param request
	 * @param response
	 * @param id
	 */

	@RequestMapping(method = RequestMethod.POST, value = "/findVisitorCheckIn")
	public void findVisitorCheckIn(HttpServletRequest request,
			HttpServletResponse response, String id,String weixinId,String nickName) {
		ResultVo resultVo = new ResultVo();
		try {
			VisitorCheckIn hrHouses=null;
			 hrHouses = visitorCheckInService.getVisitorCheckIn(id, weixinId);
			resultVo.setSuccess(true);
			resultVo.setData(hrHouses);
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("查看楼盘信息失败");
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	/**
	 * 访客登记删除
	 * 
	 * @param request
	 * @param response
	 * @param ids
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/deleteVisitorCheckInList")
	public void deleteVisitorCheckInList(HttpServletRequest request,
			HttpServletResponse response, String ids) {
		ResultVo resultVo = new ResultVo();
		try {
			visitorCheckInService.deleteObjecList("VisitorCheckIn", ids);
			resultVo.setSuccess(true);
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,Constants.LOG_DEL, "访客登记删除",
					AuthUtil.getUserid(request), "访客登记[" + ids + "]");
			log.info("访客登记 删除成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("访客登记 删除失败");
			log.info("访客登记 删除失败!");
			e.printStackTrace();
		}

		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	
	/**
	 * 已经来访，改变相应来访状态。
	 * @param request
	 * @param response
	 * @param id
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/haveVisitorCheckIn")
	public void haveVisitorCheckIn(HttpServletRequest request,
			HttpServletResponse response, String id) {
		ResultVo resultVo = new ResultVo();
		try {
			VisitorCheckIn  visitorCheckIn = (VisitorCheckIn)visitorCheckInService.findObject(VisitorCheckIn.class, id);
			visitorCheckIn.setVerifyStatus("0");
			visitorCheckIn.setVerifyTime(new Date());
			visitorCheckInService.updateObject(visitorCheckIn);
			resultVo.setSuccess(true);
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,Constants.LOG_UPDATE, "访客登记已来访",
					AuthUtil.getUserid(request), "已来访访客[" + visitorCheckIn.getName() + "]");
			log.info("访客登记 已来访成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("访客登记 已来访失败");
			log.info("访客登记 已来访失败!");
			e.printStackTrace();
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
}
